package com.student.bean;

import java.io.*;
import java.util.Scanner;

public class LogInStudent {
    private String account;
    private String password;

    public void enroll() throws IOException {
        System.out.println("----------注册界面----------");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账号");
        account = sc.next();
        System.out.println("请输入密码");
        password = sc.next();
        while (true) {
            if (password.length() > 16 || password.length() < 8) {
                System.out.println("密码长度需不少于8位，不超过16位。");
                password = sc.next();
            } else {
                break;
            }
        }
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file/Account.txt"));
        bufferedWriter.write(account);
        bufferedWriter.newLine();
        bufferedWriter.write(password);
        bufferedWriter.close();
    }

    public void login() throws IOException {
        System.out.println("----------登录界面----------");
        BufferedReader bufferedReader = new BufferedReader(new FileReader("file/Account.txt"));
        account = bufferedReader.readLine();
        password = bufferedReader.readLine();
        bufferedReader.close();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入帐号");
        String account1 = scanner.next();
        System.out.println("请输入密码");
        String password1 = scanner.next();
        for (int i = 0; i < 3; i++) {
            if (account1.equals(account) && password1.equals(password)) {
                System.out.println("登录成功");
                break;
            } else {
                System.out.println("帐号或密码输入错误，请重新输入");
                System.out.println("您还有" + (3 - i) + "次机会");
                System.out.println("请输入帐号");
                account1 = scanner.next();
                System.out.println("请输入密码");
                password1 = scanner.next();
                if (i == 3) {
                    System.out.println("如果忘记密码请重新注册");
                    System.out.println("是否重新注册Y/N");
                    String string = scanner.next();
                    if (string.equalsIgnoreCase("Y")) {
                        enroll();
                        login();
                    } else {
                        System.exit(0);
                    }

                }

            }
        }

    }
}
